/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import Classes.EstadoClass;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author Carlos Schaefer
 */
public class EstadoDAO {
    private Session session;
    
    public EstadoDAO(){
            session = Util.HibernateUtil.getSessionFactory().openSession(); 
            }
    
    public void insert(EstadoClass uf){
        Transaction t = session.beginTransaction();
        session.save(uf);
        t.commit();
        }
    
    public void update(EstadoClass uf){
        Transaction t = session.beginTransaction();
        session.merge(uf);//mesmo que o update
        t.commit();
        }
    public void delete(EstadoClass uf){
        Transaction t = session.beginTransaction();
        session.delete(uf);//mesmo que o update
        t.commit();
        }
    //buscar uma marca
    public EstadoClass findbyid(int id){
        return (EstadoClass)session.load(EstadoClass.class, id);//loadsó funciona com a chave primaria
        }
    //Buscar todos os produtos
    public List<EstadoClass> findall(){
        Query qu = session.createQuery("From EstadoClass");
        return qu.list();
        }
    
    public List<EstadoClass>findAllCli(String est){
        Query qu = session.createQuery("From EstadoClass where nome like '%"+est+"%'");
        return qu.list();
    }
    
    public List<EstadoClass> findByHQL (String hql, List params){
        Query q = this.session.createQuery(hql);
        
        int i = 0;
        for (Object ps: params){
            q.setParameter(i++, ps);            
        }
        return q.list();
        
    }
    
}
